/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.reportes;

import com.uca.sisbec.beans.ReporteGastosXBecBean;
import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.reportes.ReporteDeGastosPorBecForm;
import com.uca.sisbec.helpers.manto.BecarioHelper;
import com.uca.sisbec.helpers.reportes.ReporteGastosHelper;
import com.uca.sisbec.mapping.Becario;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Alex
 */
public class ReporteDeGastosPorBecAction extends HibernateAction {
    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReporteDeGastosPorBecForm rpgf = (ReporteDeGastosPorBecForm) form;
        rpgf.reset(mapping, request);
        return mapping.findForward("success");
    }
    
    public ActionForward ConstruirReporteDeGastosPorBecario(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        ReporteDeGastosPorBecForm rpgf = (ReporteDeGastosPorBecForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        ReporteGastosHelper rpgh = new ReporteGastosHelper(getSession(request));
        
        if(rpgf.getFechaIni().trim().equals("") || rpgf.getFechaFin().trim().equals("")){
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("fechas.vacias"));
            request.setAttribute("errors", errors);
        } else {
            SimpleDateFormat formatoDeFecha = new SimpleDateFormat("yyyy-mm-dd");
            Date fechaIni = formatoDeFecha.parse(rpgf.getFechaIni());
            Date fechaFin = formatoDeFecha.parse(rpgf.getFechaFin());
            if(rpgf.getCarnet().trim().equals("")){
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("carnet.requerido"));
                request.setAttribute("errors", errors);
            } else {
                if (fechaFin.before(fechaIni) == true) {
                    errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("intervalfecha.invalido"));
                    request.setAttribute("errors", errors);
                } else {
                    List<ReporteGastosXBecBean> reporte = rpgh.ConstruirReporteGastosXBec(rpgf);
                    BecarioHelper bh = new BecarioHelper(getSession(request));
                    Becario b = bh.buscarBecarioDB(rpgf.getCarnet());
                    msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reporte.creado"));
                    request.setAttribute("msg", msg);
                    request.setAttribute("reporte", reporte);
                    request.setAttribute("nombreCompletoBec", b.getNombres() + " " + b.getApellidos());
                }
            }
        }
        request.setAttribute("tipog", rpgf.getTipoGasto());
        return mapping.findForward("success");
    }
}
